package com.rf.richfitwheel.admin.sys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.dao.SysStationProcessConfigMapper;
import com.rf.richfitwheel.admin.sys.model.*;
import com.rf.richfitwheel.admin.sys.service.SysStationProcessConfigService;
import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.admin.utils.JacksonJsonUtil;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("sysStationProcessConfigService")
public class SysStationProcessConfigServiceImpl implements SysStationProcessConfigService {
    @Autowired
    private SysStationProcessConfigMapper sysStationProcessConfigMapper;//注入dao

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws IOException {
        PageUtils pageUtils = PageUtils.getPageInfo(params);
        Page<Station> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
        String jsonStr = JSONObject.fromObject(params.get("dataForm")).toString();
        SysStationProcessConfig sysStationProcessConfig = (SysStationProcessConfig) JacksonJsonUtil.jsonToObj(new SysStationProcessConfig(), jsonStr);
        SysStationProcessConfigExample example = new SysStationProcessConfigExample();
        SysStationProcessConfigExample.Criteria criteria = example.createCriteria();
        if (sysStationProcessConfig != null) {
//            if (StringUtils.isNotBlank(sysStationProcessConfig.getOrgId())) {
//                criteria.andOrgIdEqualTo(sysStationProcessConfig.getOrgId());
//            }
//            if (StringUtils.isNotBlank(sysStationProcessConfig.getStationCode())) {
//                criteria.andStationCodeEqualTo(sysStationProcessConfig.getStationCode());
//            }
//            if (StringUtils.isNotBlank(sysStationProcessConfig.getStationName())) {
//                criteria.andStationNameLike("%" + sysStationProcessConfig.getStationName().trim() + "%");
//            }
        }
        example.setOrderByClause(" approval_order  ");
        List<SysStationProcessConfig> list = sysStationProcessConfigMapper.selectByExample(example);
        pageUtils = new PageUtils(p);
        return pageUtils;
    }

    @Override
    public SysStationProcessConfig selectById(String id) {
        return sysStationProcessConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysStationProcessConfig sysStationProcessConfig) {

        sysStationProcessConfigMapper.insert(sysStationProcessConfig);
    }

    @Override
    public void update(SysStationProcessConfig sysStationProcessConfig) {
        sysStationProcessConfigMapper.updateByPrimaryKeySelective(sysStationProcessConfig);
    }

    @Override
    public void deleteBatch(String[] ids) {
        for (String id : ids) {
            //删除岗位资源关联
//			sqlAdapterMapper.deleteSQL("delete from sys_station_menu where station_id='" + id + "'");
            SysStationProcessConfigExample example = new SysStationProcessConfigExample();
            SysStationProcessConfigExample.Criteria criteria = example.createCriteria();
            criteria.andStationIdEqualTo(id);
            sysStationProcessConfigMapper.deleteByExample(example);
            //删除岗位用户关联
//			sqlAdapterMapper.deleteSQL("delete from sys_user_station where station_id='" + id + "'");
            SysStationProcessConfigExample example1 = new SysStationProcessConfigExample();
            SysStationProcessConfigExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andStationIdEqualTo(id);
            sysStationProcessConfigMapper.deleteByExample(example1);
            sysStationProcessConfigMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysStationProcessConfig(SysStationProcessConfig sysStationProcessConfig, UserVO userVO) {
        Date nowDate = new Date();
        sysStationProcessConfig.setId(UuidUtil.get32UUID());
        sysStationProcessConfig.setCreater(userVO.getId());
        sysStationProcessConfig.setCreateTime(nowDate);
        sysStationProcessConfig.setModTime(nowDate);
        sysStationProcessConfig.setModUser(userVO.getId());
        sysStationProcessConfig.setVersion(0);
        sysStationProcessConfigMapper.insert(sysStationProcessConfig);
    }

    @Override
    public void updateSysStationProcessConfig(SysStationProcessConfig sysStationProcessConfig, UserVO userVO) {
        Date dateNow = new Date();
        SysStationProcessConfig record = sysStationProcessConfigMapper.selectByPrimaryKey(sysStationProcessConfig.getId());
        sysStationProcessConfig.setVersion(record.getVersion() + 1);
        record.setModUser(userVO.getId());
        record.setModTime(dateNow);
        sysStationProcessConfigMapper.updateByPrimaryKeySelective(record);
    }

}